import Vue from "vue";

// 这里只负责购物车
export default ({
    namespaced: true,
    state() {
        return {
            countQuantity: 0,
            supplierList: [],
        }
    },
    mutations: {
        initSupplierList(state, supplierList) {    
            state.supplierList = supplierList;
            if(supplierList){
                if(supplierList.length==0){
                    state.countQuantity=0
                }else{
                    let countQuantity = 0;//有多少种商品
                    supplierList.forEach(store => {
                        countQuantity += store.productList.length;
                        state.countQuantity = countQuantity;
                    })
                }
            }
        },
    },
    getters: {
        countTotal(state) {
            let countPrice = 0;//总价
            let countQuantity = 0;//有多少种商品
            if(state.supplierList){
                state.supplierList.forEach(store => {
                    countQuantity += store.productList.length;
                    state.countQuantity = countQuantity;
                    for (let i = 0; i < store.productList.length; i++) {
                        if (store.productList[i].check) {
                            countPrice += store.productList[i].price * store.productList[i].quantity
                        }
                    }
                })
            }
            countPrice = countPrice.toFixed(2);
            return {
                countPrice,
                countQuantity
            }
        }
    }
})